package com.itkjb.leisurely.resource.sys.service.support;

import com.itkjb.leisurely.core.base.BaseModelWithOperationFields;
import com.itkjb.leisurely.core.model.RequestUserInfoModel;
import com.itkjb.leisurely.core.util.RequestUserInfoUtil;
import com.itkjb.leisurely.resource.common.SysConstants;
import com.itkjb.leisurely.resource.sys.model.SysUserDepModel;
import com.itkjb.leisurely.resource.sys.service.ISysDepartmentService;
import com.itkjb.leisurely.resource.sys.service.ISysUserDepService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Copyright (C), 2021-2099
 *
 * @Author: lix
 * @Date: 2021/4/12 5:24 下午
 * @Description: 部门id 工具类
 * @Version: V1.0.0
 */
@Component
public class DepIdsUtil {


    private static ISysDepartmentService departmentService;

    private static ISysUserDepService userDepService;

    @Autowired
    public void setDepartmentService(ISysDepartmentService departmentService) {
        if(DepIdsUtil.departmentService == null){
            DepIdsUtil.departmentService = departmentService;
        }

    }
    @Autowired
    public void setUserDepService(ISysUserDepService userDepService) {
        if(DepIdsUtil.userDepService == null){
            DepIdsUtil.userDepService = userDepService;
        }

    }

    /**
     * 获取 指定组织 id的所有下级组织id
     * @param depId
     * @return
     */
    public static Set<String> getSubDepIds(String depId){
        return departmentService.getIdAndSubIds(depId);
    }

    /**
     * 在用户的当前工作组织下，构建数据权限查询条件model,
     * @param param
     */
    public static void buildModelWithCurrentDepIds(BaseModelWithOperationFields param){
        if(param !=null ){
            RequestUserInfoModel user = RequestUserInfoUtil.getRequestUserInfoModel();
            // 如果数据权限是自生
            if(user !=null && user.getDataLevel() == SysConstants.DataLevel.user.getKey()){
                // 设置查询条件为自己创建的数据
                param.setCreateUserId(user.getUserId());
            }else if(user !=null && user.getDataLevel() == SysConstants.DataLevel.curDep.getKey() && StringUtils.isNotBlank(user.getCurrentDepId()) ){
                // 查询本部门数据
                Set<String> depids = new HashSet<>(1);
                // 设置查询的部门id 为本部门id
                depids.add(user.getCurrentDepId());
                param.set_depIds(depids);
            }else if(user !=null && user.getDataLevel() == SysConstants.DataLevel.curDepAndSub.getKey() && StringUtils.isNotBlank(user.getCurrentDepId()) ){
                // 查询本部门以及下级部门数据，设置本部门id以及下级id
                param.set_depIds(getSubDepIds(user.getCurrentDepId()));
            }
        }
    }

    /**
     * 构建用户所有所在组织的 数据权限查询条件model
     * @param param
     */
    public static void buildModelWithAllPermissionDepIds(BaseModelWithOperationFields param){
        if(param !=null ){
            Set<String> allPermissionDepIds = getAllPermissionDepIds();
            if(allPermissionDepIds!=null && allPermissionDepIds.size()>0){
                param.set_depIds(allPermissionDepIds);
            }
        }
    }

    /**
     * 获取用户在当前组织下，所具备的数据的最大权限的组织ids
     * @return
     */
    public static Set<String> getCurrentDepIds(){
        Set<String> depIds = new HashSet<>();
        RequestUserInfoModel user = RequestUserInfoUtil.getRequestUserInfoModel();
        // 如果数据权限是自生
        if(user !=null && user.getDataLevel() == 2 && StringUtils.isNotBlank(user.getCurrentDepId()) ){
            // 查询本部门数据
            // 设置查询的部门id 为本部门id
            depIds.add(user.getCurrentDepId());
        }else if(user !=null && user.getDataLevel() == 4 && StringUtils.isNotBlank(user.getCurrentDepId()) ){
            // 查询本部门以及下级部门数据，设置本部门id以及下级id
            depIds.addAll(getSubDepIds(user.getCurrentDepId()));
        }
        return depIds;
    }
    /**
     * 获取用户所有组织下，所具备的数据的最大权限的组织ids
     * @return
     */
    public static Set<String> getAllPermissionDepIds(){
        Set<String> depIds = new HashSet<>();
        RequestUserInfoModel user = RequestUserInfoUtil.getRequestUserInfoModel();
        // 如果数据权限是自生
        if(user !=null && user.getDepIds() !=null && !user.getDepIds().isEmpty()){
            List<SysUserDepModel> userDeps = userDepService.getUserDepsInfo(user.getUserId());
            if(userDeps !=null){
                for (SysUserDepModel userDep : userDeps) {
                    if(userDep.getDataLevel() == SysConstants.DataLevel.curDep.getKey()){
                        depIds.add(userDep.getDepId());
                    } else if (userDep.getDataLevel() == SysConstants.DataLevel.curDepAndSub.getKey()) {
                        depIds.addAll(getSubDepIds(userDep.getDepId()));
                    }
                }
            }
        }
        return depIds;
    }
}
